package br.com.educa.modelo.financeiro;

import br.com.educa.modelo.security.Usuario;
import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.PersistenceContext;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.TypedQuery;
import javax.persistence.Version;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.json.RooJson;
import org.springframework.roo.addon.serializable.RooSerializable;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.transaction.annotation.Transactional;

@Entity
@Table
@Configurable
@RooSerializable
@RooJson
@RooJavaBean
@RooToString
@RooJpaActiveRecord(finders = { "findCaixasesByUsuario", "findCaixasesByUsuarioOpen" })
public class Caixas implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @Version
    @Column(name = "version")
    private Integer version;

    @ManyToOne
    @JoinColumn(name = "usuario", referencedColumnName = "id")
    private Usuario usuario;

    @Temporal(TemporalType.DATE)
    @DateTimeFormat(style = "M-")
    private Date dataAbertura;

    private Double valorAbertura;

    @Temporal(TemporalType.DATE)
    @DateTimeFormat(style = "M-")
    private Date dataFechamento;

    private Double valorFechamento;

    @ManyToOne
    @JoinColumn(name = "conta", referencedColumnName = "id")
    private PlanoContas conta;

    private Integer status;

    private Integer autorizado;

    @PersistenceContext
    transient EntityManager entityManager;

    private static final long serialVersionUID = 1L;

    public String toString() {
        return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Integer getVersion() {
        return this.version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }

    public static final EntityManager entityManager() {
        EntityManager em = new Caixas().entityManager;
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
        return em;
    }

    public static long countCaixases() {
        return entityManager().createQuery("SELECT COUNT(o) FROM Caixas o", Long.class).getSingleResult();
    }

    public static List<br.com.educa.modelo.financeiro.Caixas> findAllCaixases() {
        return entityManager().createQuery("SELECT o FROM Caixas o", Caixas.class).getResultList();
    }

    public static br.com.educa.modelo.financeiro.Caixas findCaixas(Long id) {
        if (id == null) return null;
        return entityManager().find(Caixas.class, id);
    }

    public static List<br.com.educa.modelo.financeiro.Caixas> findCaixasEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("SELECT o FROM Caixas o", Caixas.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

    @Transactional
    public void persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.persist(this);
    }

    @Transactional
    public void remove() {
        if (this.entityManager == null) this.entityManager = entityManager();
        if (this.entityManager.contains(this)) {
            this.entityManager.remove(this);
        } else {
            Caixas attached = Caixas.findCaixas(this.id);
            this.entityManager.remove(attached);
        }
    }

    @Transactional
    public void flush() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.flush();
    }

    @Transactional
    public void clear() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.clear();
    }

    @Transactional
    public br.com.educa.modelo.financeiro.Caixas merge() {
        if (this.entityManager == null) this.entityManager = entityManager();
        Caixas merged = this.entityManager.merge(this);
        this.entityManager.flush();
        return merged;
    }

    public Usuario getUsuario() {
        return this.usuario;
    }

    public void setUsuario(Usuario usuario) {
        this.usuario = usuario;
    }

    public Date getDataAbertura() {
        return this.dataAbertura;
    }

    public void setDataAbertura(Date dataAbertura) {
        this.dataAbertura = dataAbertura;
    }

    public Double getValorAbertura() {
        return this.valorAbertura;
    }

    public void setValorAbertura(Double valorAbertura) {
        this.valorAbertura = valorAbertura;
    }

    public Date getDataFechamento() {
        return this.dataFechamento;
    }

    public void setDataFechamento(Date dataFechamento) {
        this.dataFechamento = dataFechamento;
    }

    public Double getValorFechamento() {
        return this.valorFechamento;
    }

    public void setValorFechamento(Double valorFechamento) {
        this.valorFechamento = valorFechamento;
    }

    public PlanoContas getConta() {
        return this.conta;
    }

    public void setConta(PlanoContas conta) {
        this.conta = conta;
    }

    public Integer getStatus() {
        return this.status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public Integer getAutorizado() {
        return this.autorizado;
    }

    public void setAutorizado(Integer autorizado) {
        this.autorizado = autorizado;
    }

    public String toJson() {
        return new JSONSerializer().exclude("*.class").serialize(this);
    }

    public static br.com.educa.modelo.financeiro.Caixas fromJsonToCaixas(String json) {
        return new JSONDeserializer<Caixas>().use(null, Caixas.class).deserialize(json);
    }

    public static String toJsonArray(Collection<br.com.educa.modelo.financeiro.Caixas> collection) {
        return new JSONSerializer().exclude("*.class").serialize(collection);
    }

    public static Collection<br.com.educa.modelo.financeiro.Caixas> fromJsonArrayToCaixases(String json) {
        return new JSONDeserializer<List<Caixas>>().use(null, ArrayList.class).use("values", Caixas.class).deserialize(json);
    }

    public static TypedQuery<br.com.educa.modelo.financeiro.Caixas> findCaixasesByOpen() {
        EntityManager em = Caixas.entityManager();
        TypedQuery<Caixas> q = em.createQuery("SELECT o FROM Caixas AS o WHERE o.status = 0", Caixas.class);
        return q;
    }
    
    public static TypedQuery<br.com.educa.modelo.financeiro.Caixas> findCaixasesByUsuarioOpen(Usuario usuario) {
        if (usuario == null) throw new IllegalArgumentException("The usuario argument is required");
        EntityManager em = Caixas.entityManager();
        TypedQuery<Caixas> q = em.createQuery("SELECT o FROM Caixas AS o WHERE o.usuario = :usuario AND o.status = 0", Caixas.class);
        q.setParameter("usuario", usuario);
        return q;
    }

    public static TypedQuery<br.com.educa.modelo.financeiro.Caixas> findCaixasesByUsuario(Usuario usuario) {
        if (usuario == null) throw new IllegalArgumentException("The usuario argument is required");
        EntityManager em = Caixas.entityManager();
        TypedQuery<Caixas> q = em.createQuery("SELECT o FROM Caixas AS o WHERE o.usuario = :usuario AND o.status = 1", Caixas.class);
        q.setParameter("usuario", usuario);
        return q;
    }
}
